package com.zis.security.controller;

import com.alibaba.fastjson.JSONObject;
import com.zis.security.model.LoginPara;
import com.zis.security.service.IAuthService;
import com.zis.security.service.ILoginService;
import com.zis.starter.core.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登陆认证相关操作
 * Create by wangshen 2018/2/6
 */
@RestController
@RequestMapping("/api/auth")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @Autowired
    private IAuthService authService;

    /**
     * 用户登陆
     * @param loginPara
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    @ResponseBody
    public ResponseEntity login(@RequestBody LoginPara loginPara) throws Exception {
        return loginService.userLogin(loginPara);
    }


    /**
     * 用户登陆后鉴权
     * @param token
     * @return
     * @throws Exception
     */
    @GetMapping("/user/{token}")
    @ResponseBody
    public ResponseEntity userAuth(@PathVariable("token") String token) throws Exception {
        return authService.userAuthentication(token);
    }

    /**
     * 校验用户权限
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity userAuthVerify(@RequestBody JSONObject params) throws Exception {
        return authService.userAuthVerify(params);
    }

    /**
     * 修改密码
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/rest/security", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity updatePassword(@RequestBody JSONObject params) throws Exception {
        return loginService.updatePassword(params);
    }
}
